<div class="content-wrapper">
    <section class="content-header">
        <h1>{$customer?'编辑':'新增'}客户</h1>
        <ol class="breadcrumb">
            <li><a href="/admin"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">客户管理</li>
            <li class="active"><a href="/admin/itemCate/cateList">客户列表</a></li>
            <li class="active">{$customer?'编辑':'新增'}客户</li>
        </ol>
    </section>
    <section class="content">
        <div class="box">
            <div class="box-header with-border">
            </div>
            <div class="box-body">
                <form class="form-horizontal" role="form" action="javascrip:;" method="post">
                    <input type="hidden" name="id" id="id" value="{$customer.id}" />
                    <input type="hidden" name="admin_id" id="admin_id" value="{$customer.admin_id}" />
                    <input type="hidden" name="status" id="status" value="{$customer.status}" />
                    <div class="form-group">
                        <label for="level" class="col-sm-3 control-label"> 客户级别 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="level" name="level">
                                <foreach name="levelList" item="level_val" key="l_key">
                                    <option value="{$l_key}" <if condition="$l_key eq $customer.level ">selected</if>>{$level_val}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="type" class="col-sm-3 control-label"> 客户类别 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="type" name="type">
                                <foreach name="typeList" item="type_val" key="t_key">
                                    <option value="{$t_key}" <if condition="$t_key eq $customer.type ">selected</if>>{$type_val}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="company_name" class="col-sm-3 control-label"> 公司名称<span style="color: #f00;">*</span> </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="company_name" name="company_name" value="{$customer.company_name}" <if condition="$customer.is_order eq 1">disabled readonly</if>>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="product_name" class="col-sm-3 control-label"> 推广产品<span style="color: #f00;">*</span> </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="product_name" name="product_name" value="{$customer.product_name}" <if condition="$customer.is_order eq 1">disabled readonly</if>>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label"> 联系人<span style="color: #f00;">*</span> </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="{$customer.name}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="gender" class="col-sm-3 control-label"> 联系人性别 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="gender" name="gender">
                                <option value="1" <if condition="$customer.gender eq 1 ">selected</if>>男</option>
                                <option value="2" <if condition="$customer.gender eq 2 ">selected</if>>女</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="mobile" class="col-sm-3 control-label"> 手机号</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="mobile" name="mobile" value="{$customer.mobile}" placeholder="手机号，固话，微信，QQ  至少四填一">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tel" class="col-sm-3 control-label"> 固话 </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="tel" name="tel" value="{$customer.tel}" placeholder="手机号，固话，微信，QQ  至少四填一">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="wechat" class="col-sm-3 control-label"> 微信 </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="wechat" name="wechat" value="{$customer.wechat}" placeholder="手机号，固话，微信，QQ  至少四填一">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="qq" class="col-sm-3 control-label"> QQ </label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="qq" name="qq" value="{$customer.qq}" placeholder="手机号，固话，微信，QQ  至少四填一">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cid" class="col-sm-3 control-label"> 选择一级分类 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="cid" name="cid" <if condition="$customer.is_order eq 1">disabled readonly</if>>
                                <option value="0">请选择</option>
                                <foreach name="p_cate" item="p">
                                    <option value="{$p.id}" <if condition="$p.id eq $customer.cid ">selected</if>>{$p.name}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cid" class="col-sm-3 control-label"> 选择二级分类 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="second_cid" name="second_cid" <if condition="$customer.is_order eq 1">disabled readonly</if>>
                                <option value="0">请选择</option>
                                <foreach name="two_cate" item="two">
                                    <option value="{$two.id}" <if condition="$two.id eq $customer.second_cid ">selected</if>>{$two.name}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="form-field-1" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <button type="button" class="btn btn-info btn-sm save-btn">提交</button>
                            &nbsp;
                            <button type="reset" class="btn btn-sm">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>
<script type="application/javascript">
	$('#cid').on('change',function(){
	//判断是否选取prompt属性，无返回值；
		if($(this).val()){
			var selectVal = $(this).find('option:selected').val();
			$.post("/admin/Customer/ajaxGetCate",{pid:selectVal},function(data){
				$("#second_cid").empty();
				/*$.each(data, function (index, item) {  
                  var id = data[index].id; 
                  var text = data[index].text; 
                  $("#second_cid").append("<option value='"+id+"'>"+text+"</option>");
				}); */
				$("#second_cid").html(data);
			});
		}else{
			$("#second_cid").empty();
		}
	});
    //保存用户
    $(".save-btn").click(function(){
        var preg = /^1[34578]\d{9}$/; //手机号
        var preg_tel = /^0\d{2,3}-\d{7,8}$/;
        var id = $("#id").val();
        var admin_id = $("#admin_id").val();
        var gender = $("#gender").val();
        var name = $.trim($("#name").val());
        var cid = $("#cid").val();
        var second_cid = $("#second_cid").val();
        var company_name = $("#company_name").val();
        var product_name = $("#product_name").val();
        var mobile = $("#mobile").val().trim();
        var type = $('#type').val();
        var level = $('#level').val();
        var tel = $('#tel').val().trim();
        var wechat = $('#wechat').val().trim();
        var qq = $('#qq').val().trim();
        var status = $('#status').val();

        if(name == '') {
            ArtDialogHlp.showInfo('提示', '请填写联系人');
            return false;
        }
        if(company_name == ''){
            ArtDialogHlp.showInfo('提示', '请填写公司名称');
            return false;
        }
        if(product_name == ''){
            ArtDialogHlp.showInfo('提示', '请填写推广产品名称');
            return false;
        }
        if(mobile && !preg.test(mobile)){
            ArtDialogHlp.showInfo('提示', '手机号不存在，请重新输入');
            return false;
        }
        if(tel && !preg_tel.test(tel)){
            ArtDialogHlp.showInfo('提示', '固话格式不对');
            return false;
        }
		if(mobile=='' && tel=='' && wechat=='' && qq==''){
			ArtDialogHlp.showInfo('提示', '手机号，固话，微信，QQ 至少四填一');
            return false;
		}
        if(cid == '' || cid==0){
            ArtDialogHlp.showInfo('提示', '请选择产品一级分类');
            return false;
        }
        if(second_cid == '' || second_cid==0){
            ArtDialogHlp.showInfo('提示', '请选择产品二级分类');
            return false;
        }
        $.post('/admin/Customer/ajaxSaveCustomer',
                {
                    id : id,
                    admin_id : admin_id,
                    name :name,
                    cid : cid,
                    second_cid : second_cid,
                    level : level,
                    type : type,
                    gender : gender,
                    company_name : company_name,
                    product_name : product_name,
                    mobile : mobile,
                    tel : tel,
                    wechat : wechat,
                    qq : qq,
                    status : status
                }, function(data){
                    if(data.Code == 999) {
                        ArtDialogHlp.showInfo('提示', '操作成功', function(){
                            window.location.href = '/admin/Customer/customerList';
                        });
                    } else if(data.Code == 5000) {
                        BootstrapDialog.confirm({
                            title: '提示',
                            message: data.Msg,
                            type: BootstrapDialog.TYPE_PRIMARY, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
                            closable: true, // <-- Default value is false
                            draggable: true, // <-- Default value is false
                            btnCancelLabel: '取消', // <-- Default value is 'Cancel',
                            btnOKLabel: '申领', // <-- Default value is 'OK',
                            btnOKClass: 'btn-primary', // <-- If you didn't specify it, dialog type will be used,
                            callback: function(result) {
                                // result will be true if button was click, while it will be false if users close the dialog directly.
                                if(result){
                                    $.post('/admin/Customer/ajaxAutoApplicantCustomer',
                                        {
                                            company_name : company_name,
                                            product_name : product_name,
                                            cid: cid
                                        }, function(data){
                                            if(data.Code == 999) {
                                                ArtDialogHlp.showInfo('提示', '操作成功', function(){
                                                    window.location.href = '/admin/Customer/customerList';
                                                });
                                            }else{
                                                ArtDialogHlp.showInfo('提示',data.Msg);
                                            }
                                        },"JSON");
                                }
                            }
                        });
                    }else
                    {
                        ArtDialogHlp.showInfo('提示',data.Msg);
                    }
                }, 'json');
    });
</script>